﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FrmMain;
using System.Net.NetworkInformation;

namespace MyKTV
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        Dao dao = new Dao();
        private void button1_Click(object sender, EventArgs e)
        {
            if (!comboBox1.Text.Equals(string.Empty) && !textBox2.Text.Equals(string.Empty)) {
                string sql = "select count(1) from admin_info where admin_name = '" + comboBox1.Text +
                    "' and admin_pwd = '" +textBox2.Text+"'";
                if (Convert.ToInt32( dao.DoScalar(sql)) > 0)
                {
                    MessageBox.Show("登录成功");

                    if (this.checkBox1.Checked) {
                     DateTime time=   DateTime.Now;
                           string check = " update admin_info set admin_cheack=1,admin_MAC =  '"+ stq + "',admin_lastCheack= '" + time.ToString()+"' where admin_name = '" + comboBox1.Text +
                       "' and admin_pwd = '" + textBox2.Text + "'";
                         dao.DoNonquery(check);
                      
                    }
                    this.Visible = false;
                    Form2 fr = new Form2();
                    fr.Show();
                }
                else {
                    MessageBox.Show("登录失败");
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string sql = "select * from admin_info where admin_cheack = 1 ";
            DataTable table = dao.GetTable(sql);
            this.comboBox1.DataSource = table;
            comboBox1.DisplayMember = "admin_name";
            comboBox1.ValueMember = "admin_id";
            if (comboBox1.Items.Count != 0)
            {
                if (GetBoolMac()) {   
                    string pwd = "select admin_pwd from admin_info where admin_cheack = 1  and  admin_MAC ='" + stq + "' and datediff(dd,admin_lastCheack,getdate())<7 and admin_name = '" + comboBox1.Text+"'";
                   textBox2.Text = Convert.ToString(dao.DoScalar(pwd));
                    checkBox1.Checked = true;
                }
            }
            else {
                 //  MessageBox.Show( "等于");
            }
        }
        string stq = NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString();
        private bool GetBoolMac()
        {
            
          //  MessageBox.Show(stq);
        string sql= string.Format(@" select count(1) from admin_info where admin_cheack = 1 and  admin_MAC = '" + stq+ "'  and datediff(dd,admin_lastCheack,getdate())<7");
            if ((int)dao.DoScalar(sql) > 0)
            {
                return true;
            }
           

            return false;
        }
    }
}
